package com.max.sportmanage.controller;

import com.max.sportmanage.common.Result;
import com.max.sportmanage.service.SysUserService;
import com.max.sportmanage.util.SecurityUtil;
import com.max.sportmanage.vo.LoginVo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

/**
 * @program: sport-manage-java
 * @description:登录、退出、获取登录用户信息
 * @author: max-qaq
 * @create: 2021-10-19 10:46
 **/
@Slf4j
@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    SysUserService sysUserService;


    @PostMapping("/login")
    public Result login(
            @RequestBody LoginVo loginVo
            ){
        return sysUserService.login(loginVo);
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping("/getInfo")
    public Result getUserInfo(Principal principal){
        if (null == principal){
            return Result.FAIL("请登录");
        }
        log.info("LoginController--->获取用户信息");
        return Result.SUCCESS(SecurityUtil.getUser());
    }

    @ApiOperation(value = "用户退出操作")
    @GetMapping("/logout")
    public Result userLogout(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (null != authentication){
            SecurityContextHolder.getContext().setAuthentication(null);
        }
        return Result.SUCCESS("退出成功");
    }
}
